
#include "DelphesData.h"
#include "d3pdrel16.h"
#include "E6ANA.h"
#include "TTree.h"
#include <iostream>
#include <fstream>
#include <map>
#include "Run.h"
#include "TStopwatch.h"
#include "TH1F.h"


int main(int argc, char *argv[]){
	//check for proper input arguments exit if not supplied
	
	if(argc!=4){
		std::cout<<"--------USAGE------"<<std::endl;
		std::cout<<"./e6analysis <inputRootFile> <Run.cfg> <outputRootFile>"<<std::endl;
		std::cout<<"-------------------"<<std::endl;
		exit(8);
		}


  TFile *infile=TFile::Open(TString(argv[1]),"READ");
  
  Run* myrun = new Run();//Creata an instance of the Run object
  myrun->ConfigureRun(argv[2],argv[1]);//sets up run specific parameters. Number of events etc ...
  myrun->ConfigureAnalyses(); //alocate analyses' histograms and other analysis specific settings for all analyses defined in the run config file-->analysis config file
  TTree* atree;
  infile->GetObject(myrun->TreeName.c_str(),atree);//we need the pointer to the tree we want to analyse in order to create our EvtData instance(s)

  //Now check f we got the pointer succesfully . if not give an error msg and exit...
  if(atree==NULL){
    std::cout<<	"ERROR*** cannot read tree : "<<myrun->TreeName<<" check your configurations."<<std::endl;
    exit(-8);
   }

  int Nevents=0;
  if(myrun->nevents<0) Nevents=atree->GetEntries();
  
  else Nevents=myrun->nevents;
  
  if(myrun->nevents>atree->GetEntries()){
    std::cout<<"WARNING*** supplied # of events is larger than that in the file.. Ignoring it. All events in the file will be processed instead"<<std::endl;
    Nevents=atree->GetEntries();
  }
  infile->Close();
  delete infile;
  std::cout<<"INFO*** will process : "<<Nevents<<" events"<<std::endl;
  TStopwatch timer;
  TH1F* perf_hist=new TH1F("perf_hist","time of execution",1000,0,100);
  
  unsigned int evtctr=0;
  
  for( unsigned int ievt=0;ievt<Nevents;ievt++){//event loop
	if( ievt%1000==0){
		std::cout<<"Processing event: "<<ievt<<std::endl;
        }
        //std::cout<<"kanca1"<<std::endl;
        myrun->Next(ievt);//Clear all evtdata and  fill it for ievt the entry n the tree
        timer.Start();
	myrun->RunAnalyses();//change it as myrun->RunAnalyses(); RunAnalyses() should only depend on the analysis' own EvtData !!! if we run all analyses on the same EvtData we get conflict nd funny results . This is a major bug
	timer.Stop();
	perf_hist->Fill(timer.RealTime()*1E6);
        //std::cout<<"kanca2"<<std::endl;
   evtctr++;
  }//endof event loop

TFile *pf = new TFile("performance.root","RECREATE");
perf_hist->Write();
pf->Close();
delete pf;

ifstream roofile(argv[3]);

if(roofile){
std::cout<<"Output File Already Exists.... Removing the previous file"<<std::endl;

  if(remove(argv[3])!=0){
		std::cout<<"Cannot remove previous File"<<std::endl;		
	}
	else{
		std::cout<<"Deleted Previous File"<<std::endl;
	}
}

myrun->Finalize(TString(argv[3]));

std::cout<<"Results written to the file : "<<argv[3]<<std::endl;
std::cout<<"Processed : "<<evtctr<<" events."<<std::endl; 
return 0;
}
